# 初始化
import os

# import django
# from pathlib import Path

# # Build paths inside the project like this: BASE_DIR / 'subdir'.
# BASE_DIR = Path(__file__).resolve().parent.parent
# os.environ['DJANGO_SETTINGS_MODULE'] = 'dvadmin.application.settings'
# #os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings')
# django.setup()

from dvadmin.utils.core_initialize import CoreInitialize
from application import settings
from dvadmin.system.models import MenuButton, Menu
from dvadmin.utils.core_initialize import CoreInitialize


class Initialize(CoreInitialize):
    creator_id = "456b688c-8ad5-46de-bc2e-d41d8047bd42"

    def init_menu(self):
        """
        初始化菜单表
        """
        self.menu_data = [
            {"id": "b02a2224-efc9-4329-9033-7380ac38fb20", "name": "定时任务", "sort": 8, "icon": "hourglass-2",
             "web_path": "/task", "component": "dvadmin_plugins/dvadmin_apscheduler_web/task/index",
             "component_name": "task", "parent_id": "54f769b0-3dff-416c-8102-e55ec44827cc"},
            {"id": "b02a2224-efc9-4329-9033-7380ac38fb21", "name": "定时任务详情", "sort": 8, "icon": "map-o",
             "web_path": "/taskDetail", "component": "dvadmin_plugins/dvadmin_apscheduler_web/taskDetail/index",
             "component_name": "taskDetail", "parent_id": "54f769b0-3dff-416c-8102-e55ec44827cc", "visible": 0},
        ]
        self.save(Menu, self.menu_data, "菜单表")

    def init_menu_button(self):
        """
        初始化菜单权限表
        """
        self.menu_button_data = [
            {'id': 'ca8125a6-2868-435b-a6c6-99ce1ef5d9e4', 'menu_id': 'b02a2224-efc9-4329-9033-7380ac38fb20',
             'name': '查询', 'value': 'Search', 'api': '/api/dvadmin_apscheduler/cron/', 'method': 0},
            {'id': '74224c4d-52fe-4624-bf32-5dc432199803', 'menu_id': 'b02a2224-efc9-4329-9033-7380ac38fb20',
             'name': '新增', 'value': 'Create', 'api': '/api/dvadmin_apscheduler/cron/', 'method': 1},
            {'id': '399cd210-fa76-461b-aa5d-8be40b71b3f6', 'menu_id': 'b02a2224-efc9-4329-9033-7380ac38fb20',
             'name': '编辑', 'value': 'Update', 'api': '/api/dvadmin_apscheduler/cron/{id}/', 'method': 2},
            {'id': '64352645-ae18-46be-87b0-e8dc8137a645', 'menu_id': 'b02a2224-efc9-4329-9033-7380ac38fb20',
             'name': '删除', 'value': 'Delete', 'api': '/api/dvadmin_apscheduler/cron/{id}/', 'method': 3},
            {'id': '70a17263-00fa-4299-b9dc-df84f54cafc6', 'menu_id': 'b02a2224-efc9-4329-9033-7380ac38fb20',
             'name': '单例', 'value': 'Retrieve', 'api': '/api/dvadmin_apscheduler/cron/{id}/', 'method': 0},
            {'id': 'ca3125a6-2868-435b-a6c6-99ce1ef5d9e4', 'menu_id': 'b02a2224-efc9-4329-9033-7380ac38fb21',
             'name': '查询', 'value': 'Search', 'api': '/api/dvadmin_apscheduler/task/', 'method': 0},
            {'id': '74424c4d-52fe-4624-bf32-5dc432199803', 'menu_id': 'b02a2224-efc9-4329-9033-7380ac38fb21',
             'name': '新增', 'value': 'Create', 'api': '/api/dvadmin_apscheduler/task/', 'method': 1},
            {'id': '397cd210-fa76-461b-aa5d-8be40b71b3f6', 'menu_id': 'b02a2224-efc9-4329-9033-7380ac38fb21',
             'name': '编辑', 'value': 'Update', 'api': '/api/dvadmin_apscheduler/task/{id}/', 'method': 2},
            {'id': '64852645-ae18-46be-87b0-e8dc8137a645', 'menu_id': 'b02a2224-efc9-4329-9033-7380ac38fb21',
             'name': '删除', 'value': 'Delete', 'api': '/api/dvadmin_apscheduler/task/{id}/', 'method': 3},
            {'id': '70017263-00fa-4299-b9dc-df84f54cafc6', 'menu_id': 'b02a2224-efc9-4329-9033-7380ac38fb21',
             'name': '单例', 'value': 'Retrieve', 'api': '/api/dvadmin_apscheduler/task/{id}/', 'method': 0}]

        # value = {"Search": "查询", "Create": "新增", "Update": "编辑", "Delete": "删除", "Retrieve": "单例"}
        # import uuid
        # for menu in self.menu_data:
        #     for index, val in enumerate(value.items()):
        #         api = menu.get("component")
        #         if not api: continue
        #         api = api.strip('/')
        #         new_api = ""
        #         for ele in api:
        #             if ele.isupper():
        #                 new_api += f"_{ele.lower()}"
        #             else:
        #                 new_api += ele
        #         self.menu_button_data.append(
        #             {
        #                 "id": str(uuid.uuid4()),
        #                 "menu_id": menu.get("id"),
        #                 "name": val[1],
        #                 "value": val[0],
        #                 "api": '/api/' + new_api + '/{id}/' if val[0] in ["Update", "Delete", "Retrieve"] else '/',
        #                 "method": 0 if index == 4 else index}
        #         )
        # print(1, self.menu_button_data)
        self.save(MenuButton, self.menu_button_data, "菜单权限表")

    def migrate(self):
        from django.core import management
        management.call_command('makemigrations', "gc_pms_backend")
        command = 'migrate'
        if not getattr(settings, 'PLUGINS_LIST', {}).get('gc_pms_backend', None):
            command = 'migrate_schemas'
        management.call_command(command, "gc_pms_backend")

    def run(self):
        self.init_menu()
        self.init_menu_button()


def main(reset=False, is_migrate=False):
    init = Initialize(reset)
    #init.run()
    print("oooooooo")
    if is_migrate:
        init.migrate()
        print("okkkkk")


if __name__ == '__main__':
    main()
